package dao;

import entity.User;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import utils.HibernateUtils;

public class UserDaoImpl implements UserDao{

    @Override
    public User Login(String uname, String upasd) {
        Session session = HibernateUtils.getSession();
        Query query = session.createQuery("from entity.User u where u.email = ? and u.upassword = ? and u.isDelete = 0");
        query.setParameter(0,uname);
        query.setParameter(1,upasd);
        return (User) query.uniqueResult();
    }

    @Override
    public int Register(User user) {
        Session session = null;
        Transaction tx = null;
        try {
            session = HibernateUtils.getSession();
            tx = session.beginTransaction();
            session.save(user);
            tx.commit();
            return 1;
        }catch (Exception e){
            tx.rollback();
            e.printStackTrace();
            return 0;
        }finally {
            HibernateUtils.closeSession();
        }
    }
}
